package com.rednose.videoplayer.dialogs;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatDialogFragment;

import com.bumptech.glide.load.engine.cache.DiskLruCacheWrapper;
import com.rednose.videoplayer.R;

public class BrightnessDialog extends AppCompatDialogFragment {
    private TextView bright_no;
    private ImageView cross;
    private SeekBar seekBar;

    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());            //创建一个AlertDialog.Builder对象，用于构建对话框
        LayoutInflater inflater = getActivity().getLayoutInflater();                     //获取LayoutInflater对象，用于加载布局
        View view = inflater.inflate(R.layout.brt_dialog_item, null);               //加载brt_dialog_item布局
        builder.setView(view);                                                           //设置对话框视图为brt_dialog_item布局

        cross = view.findViewById(R.id.brt_close);                                        //从布局文件中获取关闭亮度对话框的ImageView
        bright_no = view.findViewById(R.id.brt_number);                                   //从布局文件中获取显示亮度百分比的TextView
        seekBar = view.findViewById(R.id.brt_seekbar);                                    //从布局文件中获取亮度调节的SeekBar
        int brightness = Settings.System.getInt(getContext().getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS, 0);                                  //获取系统亮度
        bright_no.setText(brightness + "");                                              //设置亮度百分比
        seekBar.setProgress(brightness);                                                  //设置SeekBar的当前值为系统亮度

        //设置SeekBar的监听器,用于监听SeekBar的进度改变事件,当SeekBar的进度改变时，设置系统亮度
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)                                               //指定该方法需要API 23及以上的版本
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {        //当SeekBar的进度改变时,设置系统亮度
                Context context = getContext().getApplicationContext();                             //获取应用程序的上下文
                boolean canWrite = Settings.System.canWrite(context);                               //检查是否有权限写入系统设置
                if (canWrite) {                                                                     //如果有权限写入系统设置
                    int sBrightness = progress * 255 / 255;                                         //计算亮度值
                    bright_no.setText(sBrightness + "");                                            //设置亮度百分比
                    Settings.System.putInt(context.getContentResolver(),                            //设置系统亮度模式为手动
                            Settings.System.SCREEN_BRIGHTNESS_MODE,
                            Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
                    Settings.System.putInt(context.getContentResolver(),                            //设置系统亮度
                            Settings.System.SCREEN_BRIGHTNESS, sBrightness);
                } else {                                                                            //如果没有权限写入系统设置
                    Toast.makeText(context, "无系统权限", Toast.LENGTH_SHORT).show(); //提示用户开启权限
                    Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
                    intent.setData(Uri.parse("package:" + context.getPackageName()));        //设置Intent的数据
                    startActivityForResult(intent, 0);
                }
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }

        });
        cross.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        return builder.create();
    }

}
